package com.ruoyi.quality.controller;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.quality.domain.QualityCustom;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.quality.domain.QualitySupplier;
import com.ruoyi.quality.service.IQualitySupplierService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 供应商管理Controller
 *
 * @author wky
 * @date 2024-06-05
 */
@RestController
@RequestMapping("/quality/supplier")
@Tag(name = "【供应商管理】管理")
public class QualitySupplierController extends BaseController
{
    @Autowired
    private IQualitySupplierService qualitySupplierService;

    /**
     * 查询供应商管理列表
     */
    @Operation(summary = "查询供应商管理列表")
    @PreAuthorize("@ss.hasPermi('quality:supplier:list')")
    @GetMapping("/list")
    public TableDataInfo list(QualitySupplier qualitySupplier)
    {
        startPage();
        List<QualitySupplier> list = qualitySupplierService.selectQualitySupplierList(qualitySupplier);
        return getDataTable(list);
    }
    @Operation(summary = "查询客户管理列表")
    @PreAuthorize("@ss.hasPermi('quality:custom:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll()
    {
        List<QualitySupplier> list = qualitySupplierService.selectQualitySupplierList(null);
        return AjaxResult.success(list);
    }
    /**
     * 导出供应商管理列表
     */
    @Operation(summary = "导出供应商管理列表")
    @PreAuthorize("@ss.hasPermi('quality:supplier:export')")
    @Log(title = "供应商管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QualitySupplier qualitySupplier)
    {
        List<QualitySupplier> list = qualitySupplierService.selectQualitySupplierList(qualitySupplier);
        ExcelUtil<QualitySupplier> util = new ExcelUtil<QualitySupplier>(QualitySupplier.class);
        util.exportExcel(response, list, "供应商管理数据");
    }

    /**
     * 获取供应商管理详细信息
     */
    @Operation(summary = "获取供应商管理详细信息")
    @PreAuthorize("@ss.hasPermi('quality:supplier:query')")
    @GetMapping(value = "/{supplierId}")
    public AjaxResult getInfo(@PathVariable("supplierId") Long supplierId)
    {
        return success(qualitySupplierService.selectQualitySupplierBySupplierId(supplierId));
    }

    /**
     * 新增供应商管理
     */
    @Operation(summary = "新增供应商管理")
    @PreAuthorize("@ss.hasPermi('quality:supplier:add')")
    @Log(title = "供应商管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QualitySupplier qualitySupplier)
    {
        return toAjax(qualitySupplierService.insertQualitySupplier(qualitySupplier));
    }

    /**
     * 修改供应商管理
     */
    @Operation(summary = "修改供应商管理")
    @PreAuthorize("@ss.hasPermi('quality:supplier:edit')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QualitySupplier qualitySupplier)
    {
        return toAjax(qualitySupplierService.updateQualitySupplier(qualitySupplier));
    }

    /**
     * 删除供应商管理
     */
    @Operation(summary = "删除供应商管理")
    @PreAuthorize("@ss.hasPermi('quality:supplier:remove')")
    @Log(title = "供应商管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{supplierIds}")
    public AjaxResult remove(@PathVariable( name = "supplierIds" ) Long[] supplierIds)
    {
        return toAjax(qualitySupplierService.deleteQualitySupplierBySupplierIds(supplierIds));
    }

    @GetMapping("/getData")
    @Anonymous
    public AjaxResult getData(){
        int year = LocalDateTime.now().getYear();
        List<Map<Long, Long>> monthCountMap = qualitySupplierService.getMonthCountMap((long) year);
        return success(monthCountMap);
    }
}
